package heap;

import java.util.Random;

public class TestHeapify {

    public static void main(String[] args) {
        int n = 1000000;
        Integer[] testData = new Integer[n];
        Random random = new Random();
        for (int i = 0; i < n; i++) {
            testData[i] = (random.nextInt(Integer.MAX_VALUE));
        }
        double time1 = testHeap(testData, false);
        double time2 = testHeap(testData, true);
        System.out.println("no heapify:"+time1+ ",is heapify:"+time2);
    }

    private static double testHeap(Integer[] testData,boolean isHeap){
        long start = System.nanoTime();
        MaxHeap<Integer> maxHeap;
        if (isHeap) {
            maxHeap = new MaxHeap<>(testData);
        }else{
            maxHeap = new MaxHeap<>();
            for (int num : testData) {
                maxHeap.add(num);
            }
        }
        int[] arr = new int[testData.length];
        for (int i = 0; i < testData.length; i++) {
            arr[i] = maxHeap.extractMax();
        }
        for (int i = 1; i < testData.length; i++) {
            if (arr[i - 1] < arr[i])
                throw new RuntimeException("Error");
        }
        System.out.println("test heap compelete");
        long end = System.nanoTime();
        return (end-start)/1000000000.0;
    }
}
